package eteam.aps.systemservice.service;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import eteam.aps.systeminterface.model.dto.FlowBillClassDto;
import eteam.aps.systeminterface.model.dto.FlowOrgDto;
import eteam.aps.systeminterface.model.dto.FlowSubSystemDto;
import eteam.aps.systeminterface.model.dto.FlowUserDto;
import eteam.aps.systeminterface.model.enums.OrgTypeEnum;
import eteam.aps.systeminterface.service.IFlowController;
import eteam.aps.systemservice.bll.OrgBll;
import eteam.aps.systemservice.bll.RoleBll;
import eteam.aps.systemservice.bll.SystemBll;
import eteam.aps.systemservice.bll.UserBll;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 流程引擎基础服务
 */
@Api(tags = "流程引擎控制器")
@RestController
@RequestMapping(value = "/api/flow")
public class FlowController implements IFlowController {
    @Autowired
    private OrgBll orgBll;
    @Autowired
    private RoleBll roleBll;
    @Autowired
    private UserBll userBll;
    @Autowired
    private SystemBll systemBll;

    //region 机构相关

    /**
     * 根据机构名称获取机构Id
     *
     * @param orgName: 机构名称
     * @return: 机构Id
     */
    @ApiOperationSupport(order = 101)
    @ApiOperation(value = "根据机构名称获取机构Id")
    @ApiImplicitParam(name = "orgName", value = "机构名称")
    @Override
    @GetMapping(value = "/getorgidbyname")
    public String getOrgIdByName(@RequestParam String orgName) {
        return orgBll.getOrgIdByName(orgName);
    }

    /**
     * 根据用户名称获取机构Id
     *
     * @param userName: 用户名
     * @return: 机构Id
     */
    @ApiOperationSupport(order = 102)
    @ApiOperation(value = "根据用户名称获取机构Id")
    @ApiImplicitParam(name = "userName", value = "用户名")
    @Override
    @GetMapping(value = "/getorgidbyusername")
    public String getOrgIdByUserName(@RequestParam String userName) {
        return userBll.getOrgIdByUserName(userName);
    }

    /**
     * 获取上级机构Id
     *
     * @param orgId: 当前机构Id
     * @return: 上级机构Id
     */
    @ApiOperationSupport(order = 103)
    @ApiOperation(value = "获取上级机构Id")
    @ApiImplicitParam(name = "orgId", value = "当前机构Id")
    @Override
    @GetMapping(value = "/getaboveorgid")
    public String getAboveOrgId(@RequestParam String orgId) {
        return orgBll.getAboveOrgId(orgId);
    }

    /**
     * 获取上级机构对象
     *
     * @param orgId: 当前机构Id
     * @return: 上级机构对象
     */
    @ApiOperationSupport(order = 104)
    @ApiOperation(value = "获取上级机构对象")
    @ApiImplicitParam(name = "orgId", value = "当前机构Id")
    @Override
    @GetMapping(value = "/getaboveorg")
    public FlowOrgDto getAboveOrg(@RequestParam String orgId) {
        return orgBll.getAboveOrg(orgId);
    }

    /**
     * 获取上级代管机构Id
     *
     * @param orgId: 当前机构Id
     * @return: 上级代管机构Id
     */
    @ApiOperationSupport(order = 105)
    @ApiOperation(value = "获取上级代管机构Id")
    @ApiImplicitParam(name = "orgId", value = "当前机构Id")
    @Override
    @GetMapping(value = "/getabovesuborgid")
    public String getAboveSubOrgId(@RequestParam String orgId) {
        return orgBll.getAboveOrgId(orgId);
    }

    /**
     * 通过名称查找机构(模糊查询)
     *
     * @param orgName: 机构名称
     * @return: 机构信息列表
     */
    @ApiOperationSupport(order = 106)
    @ApiOperation(value = "通过名称查找机构(模糊查询)")
    @ApiImplicitParam(name = "orgName", value = "机构名称")
    @Override
    @GetMapping(value = "/getorglistbyname")
    public List<FlowOrgDto> getOrgListByName(@RequestParam String orgName) {
        return orgBll.getOrgListByName(orgName);
    }

    /**
     * 通过机构名称查找机构，精确查找
     *
     * @param orgName: 机构名称
     * @return: 机构信息
     */
    @ApiOperationSupport(order = 107)
    @ApiOperation(value = "通过机构名称查找机构，精确查找")
    @ApiImplicitParam(name = "orgName", value = "机构名称")
    @Override
    @GetMapping(value = "/getorgbyname")
    public FlowOrgDto getOrgByName(@RequestParam String orgName) {
        return orgBll.getOrgByName(orgName);
    }

    /**
     * 通过机构Id获取机构信息
     *
     * @param orgId: 机构Id
     * @return: 机构信息
     */
    @ApiOperationSupport(order = 108)
    @ApiOperation(value = "通过机构Id获取机构信息")
    @ApiImplicitParam(name = "orgId", value = "机构Id")
    @Override
    @GetMapping(value = "/getorgbyid")
    public FlowOrgDto getOrgByID(@RequestParam String orgId) {
        return orgBll.getOrgById(orgId);
    }

    //endregion

    //region 角色相关

    /**
     * 用户是否属于某个角色
     *
     * @param userId:   用户ID
     * @param roleName: 角色名
     * @return
     */
    @ApiOperationSupport(order = 201)
    @ApiOperation(value = "用户是否属于某个角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID"),
            @ApiImplicitParam(name = "roleName", value = "角色名")
    })
    @Override
    @GetMapping(value = "/userhasrole")
    public boolean userHasRole(@RequestParam String userId, @RequestParam String roleName) {
        return roleBll.userHasRole(userId, roleName);
    }

    /**
     * 获取所有角色名称列表(角色模板)
     *
     * @return: 角色名称列表
     */
    @ApiOperationSupport(order = 202)
    @ApiOperation(value = "获取所有角色名称列表(角色模板)")
    @Override
    @GetMapping(value = "/getrolelist")
    public List<String> getRoleList() {
        return roleBll.getRoleNameList();
    }

    /**
     * 根据机构类型获取角色名称列表(角色模板)
     *
     * @param orgType: 机构类型
     * @return: 角色名称列表
     */
    @ApiOperationSupport(order = 203)
    @ApiOperation(value = "根据机构类型获取角色名称列表(角色模板)")
    @ApiImplicitParam(name = "orgType", value = "机构类型")
    @Override
    @GetMapping(value = "/getRoleListByOrgType")
    public List<String> getRoleListByOrgType(@RequestParam OrgTypeEnum orgType) {
        return roleBll.getRoleNameListByType(orgType);
    }

    //endregion

    //region 用户相关

    /**
     * 判断用户是否存在
     *
     * @param userId: 用户ID
     * @return: 是否存在
     */
    @ApiOperationSupport(order = 301)
    @ApiOperation(value = "判断用户是否存在")
    @ApiImplicitParam(name = "userId", value = "用户ID")
    @Override
    @GetMapping(value = "/userexist")
    public boolean userExist(@RequestParam("userId") String userId) {
        return userBll.userExist(userId);
    }

    /**
     * 用户是否具有流程设计的权限
     *
     * @param userId: 用户ID
     * @return
     */
    @ApiOperationSupport(order = 302)
    @ApiOperation(value = "用户是否具有流程设计的权限")
    @ApiImplicitParam(name = "userId", value = "用户ID")
    @Override
    @GetMapping(value = "/hasflowdesignright")
    public boolean hasFlowDesignRight(@RequestParam("userId") String userId) {
        return true;
    }

    /**
     * 根据角色名称获取所有用户名称
     *
     * @param roleName: 角色名称
     * @return: 用户名列表
     */
    @ApiOperationSupport(order = 303)
    @ApiOperation(value = "根据角色名称获取所有用户名称")
    @ApiImplicitParam(name = "roleName", value = "角色名称")
    @Override
    @GetMapping(value = "/getusernamebyrolename")
    public List<String> getUserNameByRoleName(@RequestParam String roleName) {
        return userBll.getUserNameByRoleName("", roleName);
    }

    /**
     * 根据机构ID+角色名称获取所有用户名称
     *
     * @param orgId:    机构ID
     * @param roleName: 角色名称
     * @return: 用户名列表
     */
    @ApiOperationSupport(order = 304)
    @ApiOperation(value = "根据机构ID/角色名称获取所有用户名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "机构ID"),
            @ApiImplicitParam(name = "roleName", value = "角色名称")
    })
    @Override
    @GetMapping(value = "/getusernamebyorgid")
    public List<String> getUserNameByOrgId(@RequestParam String orgId, @RequestParam String roleName) {
        return userBll.getUserNameByRoleName(orgId, roleName);
    }

    /**
     * 根据机构Id获取用户ID列表
     *
     * @param orgId: 机构ID
     * @return: 用户ID列表
     */
    @ApiOperationSupport(order = 305)
    @ApiOperation(value = "获取机构的用户")
    @ApiImplicitParam(name = "orgId", value = "机构ID")
    @Override
    @GetMapping(value = "/getuseridsbyorgid")
    public List<String> getUserIdsByOrgId(@RequestParam String orgId) {
        return userBll.getUserIdByOrgId(orgId);
    }

    /**
     * 通过用户登录名、姓名、拼音码查找用户信息
     *
     * @param keyWord: 检索关键字【登录名、姓名、拼音码】
     * @return: 用户信息列表
     */
    @ApiOperationSupport(order = 306)
    @ApiOperation(value = "通过用户登录名、姓名、拼音码查找用户信息")
    @ApiImplicitParam(name = "keyWord", value = "检索关键字【登录名、姓名、拼音码】")
    @Override
    @GetMapping(value = "/getuserlistbykeyword")
    public List<FlowUserDto> getUserListByKeyWord(@RequestParam String keyWord) {
        return userBll.getUserListByKeyWord(keyWord);
    }

    /**
     * 根据机构ID+角色名称获取所有用户信息
     *
     * @param orgId:    机构ID
     * @param roleName: 角色名称
     * @return: 用户信息列表
     */
    @ApiOperationSupport(order = 307)
    @ApiOperation(value = "根据机构ID/角色名称获取所有用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "机构ID"),
            @ApiImplicitParam(name = "roleName", value = "角色名称")
    })
    @Override
    @GetMapping(value = "/getusersbyrolename")
    public List<FlowUserDto> getUsersByRoleName(@RequestParam String orgId, @RequestParam String roleName) {
        return userBll.getUserListByRoleName(orgId, roleName);
    }

    /**
     * 根据机构Id获取所有用户信息
     *
     * @param orgId: 机构ID
     * @return: 用户信息列表
     */
    @ApiOperationSupport(order = 308)
    @ApiOperation(value = "根据机构Id获取所有用户信息")
    @ApiImplicitParam(name = "orgId", value = "机构ID")
    @Override
    @GetMapping(value = "/getusersbyorgid")
    public List<FlowUserDto> getUsersByOrgId(@RequestParam String orgId) {
        return userBll.getUserListByOrgId(orgId);
    }

    /**
     * 通过用户Id获取用户信息
     *
     * @param userId: 用户ID
     * @param orgId:  机构ID
     * @return: 用户信息
     */
    @ApiOperationSupport(order = 309)
    @ApiOperation(value = "通过用户Id获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID"),
            @ApiImplicitParam(name = "orgId", value = "机构ID")
    })
    @Override
    @GetMapping(value = "/getuserbyid")
    public FlowUserDto getUserById(@RequestParam String userId, @RequestParam String orgId) {
        return userBll.getUserByIdAndOrgId(userId, orgId);
    }

    /**
     * 通过身份证号获取用户Id
     *
     * @param idCardNo: 身份证ID
     * @return: 用户ID
     */
    @ApiOperationSupport(order = 310)
    @ApiOperation(value = "通过身份证号获取用户Id")
    @ApiImplicitParam(name = "idCardNo", value = "身份证ID")
    @Override
    @GetMapping(value = "/getuseridbyidcardno")
    public String getUserIdByIDCardNo(@RequestParam String idCardNo) {
        return "";
    }

    //endregion

    //region 子系统相关

    /**
     * 获取所有子系统
     *
     * @return: 子系统列表
     */
    @ApiOperationSupport(order = 401)
    @ApiOperation(value = "获取所有子系统")
    @Override
    @GetMapping(value = "/getallsystem")
    public List<FlowSubSystemDto> getAllSystem() {
        List<FlowSubSystemDto> list = new ArrayList<>();
        list.add(new FlowSubSystemDto("1","essentialData","基本信息"));
        list.add(new FlowSubSystemDto("2","saleManage","销售管理"));
        list.add(new FlowSubSystemDto("3","purchasingManage","采购管理"));
        list.add(new FlowSubSystemDto("4","stockManage","库存管理"));
        list.add(new FlowSubSystemDto("5","productManage","生产管理"));
        list.add(new FlowSubSystemDto("6","receivablesManage","应收管理"));
        list.add(new FlowSubSystemDto("7","copingManage","应付管理"));
        list.add(new FlowSubSystemDto("9","other","其他"));
        return list;
    }

    /**
     * 获取所有单据信息
     *
     * @return: 单据列表
     */
    @ApiOperationSupport(order = 402)
    @ApiOperation(value = "获取所有单据信息")
    @Override
    @GetMapping(value = "/getallbillinfo")
    public List<FlowBillClassDto> getAllBillInfo() {
        return systemBll.getAllBillInfoList();
    }

    //endregion
}
